//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//********************************************************* 

using Microsoft.TridentWordAddIn.Common;
using Microsoft.TridentWordAddIn.ResourceHelper;
using System.Windows.Forms;

namespace Microsoft.TridentWordAddIn.Presentation
{
    /// <summary>
    /// Enter Password Dialog
    /// </summary>
    internal partial class UnprotectDocumentForm : BaseForm
    {
        bool cancelFormClose = false;
        IThisAddInPackageHost packageHost;

        /// <summary>
        /// Initializes a new instance of the <see cref="UnprotectDocumentForm"/> class.
        /// </summary>
        public UnprotectDocumentForm(IThisAddInPackageHost packageHost)
        {
            this.InitializeComponent();
            this.packageHost = packageHost;
        }

        /// <summary>
        /// Gets the password entered.
        /// </summary>
        /// <value>The password entered.</value>
        public string Password
        {
            get { return this.txtPassword.Text.Trim(); }
        }

        /// <summary>
        /// Handles the Click event of the btnOK control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnOK_Click(object sender, System.EventArgs e)
        {
            if (this.packageHost.UnprotectDocument(txtPassword.Text) == false)
            {
                MessageBoxHelper.ShowDefault(ResourceFetcher.GetString("InvalidPassword"), MessageBoxIcon.Exclamation);
                this.cancelFormClose = true;
            }
            else
            {
                this.cancelFormClose = false;
            }
        }

        /// <summary>
        /// Handles the Click event of the btnCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnCancel_Click(object sender, System.EventArgs e)
        {
            this.cancelFormClose = false;
        }

        /// <summary>
        /// Handles the FormClosing event of the UnprotectDocumentForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.FormClosingEventArgs"/> instance containing the event data.</param>
        private void UnprotectDocumentForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(e.CloseReason == CloseReason.None)
            {
                e.Cancel = this.cancelFormClose;
            }
        }
    }
}